package com.zp.base.admin.common.utils;

import com.zp.base.admin.common.constant.BizCodeEnum;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;

/**
 * @author ZhangPeng
 * @email zhangpeng98@aliyun.com
 * @create 2021-03-02 10:20
 */
public class ValidUtil {

    public static R getErrorInfo(BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        HashMap<String, Object> errorMap = new HashMap<>();
        for (FieldError fieldError : fieldErrors) {
            return R.error(BizCodeEnum.VALID_EXCEPTION.getCode(), fieldError.getDefaultMessage());
        }
        return R.error().put("data", errorMap);
    }
}
